package client.routeForm;
import client.ClientSender;
import exception.AirportNotFoundException;
import exception.InvalidArgumentsException;
import java.io.IOException;
import java.net.UnknownHostException;
import model.airport.*;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.Airport;
/**
 *Панелька для детализации информации об маршруте.
 * использовать метод setData для установки значений в поля
 * @author GeneraL
 */
public class RouteInfoPanel extends javax.swing.JPanel {

    Airport[] airports=null;
    private static final int nullId=-1;

    /**
     * Creates new form RouteInfoPanel
     */
    public RouteInfoPanel() {
        initComponents();
        reloadPorts();
    }
    /**
     * метод для заполнения данных о аэропортах с сервера и вывода в комбобоксы
     */
    public void reloadPorts(){
        try{
        ClientSender sender=new ClientSender();
        ArrayList<Airport> airportList=sender.sendPort("portlist");
        sender.close();
        this.airports=new Airport[airportList.size()+1];
        this.airports[0]=null;
        String[] unikPortName=new String[airports.length];
        unikPortName[0]="Несуществующий";
        for(int i=1;i<this.airports.length;i++){
            this.airports[i]=airportList.get(i-1);
            unikPortName[i]=new String("№"+airports[i].getId()+":"+airports[i].getName());
        }
        
        aComboBox.setModel(new javax.swing.DefaultComboBoxModel(unikPortName));
        bComboBox.setModel(new javax.swing.DefaultComboBoxModel(unikPortName));
        }catch (UnknownHostException  e){
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "Exeption",JOptionPane.ERROR_MESSAGE);
            System.err.println(e.getMessage());
            System.exit(0);
        }
        catch (IOException  e){
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "Exeption",JOptionPane.ERROR_MESSAGE);
            System.err.println(e.getMessage());
            System.exit(0);
        }
        catch (TimeoutException | AirportNotFoundException | InvalidArgumentsException  e){
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "Exeption",JOptionPane.ERROR_MESSAGE);
            System.err.println(e.getMessage());
        }
    }
    public void setData(String title,double distance,int aId,int bId){
        this.titleLabel.setText(title);
        this.distanseInput.setText(Double.toString(distance));
        distanseInput.setEnabled(true);
        aComboBox.setSelectedIndex(0);
        for(int i=1;i<airports.length;i++){
            if(airports[i].getId()==aId){
                aComboBox.setSelectedIndex(i);
                break;
            }
        }
        bComboBox.setSelectedIndex(0);
        for(int i=1;i<airports.length;i++){
            if(airports[i].getId()==bId){
                bComboBox.setSelectedIndex(i);
                break;
            }
        }
        aComboBox.setEnabled(true);
        bComboBox.setEnabled(true);
    }
    public String getRouteType(){
        return titleLabel.getText();
    }
    public double getDistance(){
        return Double.parseDouble(distanseInput.getText());

       
    }
    /**
     * 
     * @return идентификатор пункта А
     */
    public int getAId(){
        int i=aComboBox.getSelectedIndex();
        if(i==0)return nullId;
        else{
            return airports[i].getId();
        }
    }
    /**
     * 
     * @return идентификатор пункта В
     */
    public int getBId(){
        int i=bComboBox.getSelectedIndex();
        if(i==0)return nullId;
        else{
            return airports[i].getId();
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titleLabel = new javax.swing.JLabel();
        aComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        bComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        distanseInput = new javax.swing.JTextField();

        titleLabel.setText("Выберите маршрут");

        aComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "" }));
        aComboBox.setEnabled(false);

        jLabel2.setText("Пункт А:");

        jLabel3.setText("Пункт Б:");

        bComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "" }));
        bComboBox.setEnabled(false);

        jLabel4.setText("Дистанция:");

        distanseInput.setText(" ");
        distanseInput.setEnabled(false);
        distanseInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                distanseInputActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(aComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(distanseInput, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(bComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(distanseInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void distanseInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_distanseInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_distanseInputActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox aComboBox;
    private javax.swing.JComboBox bComboBox;
    private javax.swing.JTextField distanseInput;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables
}
